﻿Imports System.Data.OleDb
Public Class EditProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionStringRestaurants").ConnectionString)
            Dim SqlString As String = "SELECT * FROM Profile WHERE [UserName]=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()
            Dim dataReader As OleDbDataReader = cmd.ExecuteReader()
            'Check that there are some records and populate fields
            If dataReader.HasRows() Then
                dataReader.Read()
                If Not IsDBNull(dataReader("FirstName")) Then tb_FirstName.Text = dataReader("FirstName")
                If Not IsDBNull(dataReader("LastName")) Then tb_LastName.Text = dataReader("LastName")
                If Not IsDBNull(dataReader("Details")) Then tb_Details.Text = dataReader("Details")
                If Not IsDBNull(dataReader("MyFavouriteRestaurant")) Then tb_FavRestaurant.Text = dataReader("MyFavouriteRestaurant")
                If Not IsDBNull(dataReader("Hometown")) Then tb_Hometown.Text = dataReader("Hometown")
                If Not IsDBNull(dataReader("MyFavouriteShop")) Then tb_FavShop.Text = dataReader("MyFavouriteShop")
                If Not IsDBNull(dataReader("GlutenfreeSince")) Then tb_Glutenfreesince.Text = dataReader("GlutenfreeSince")

            End If
        End If

    End Sub

    Protected Sub btn_Update_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Update.Click
        'Save profile changes into Database'
        Dim newPictureName As String = ""
        Dim SqlString As String

        'check if new image is being uploaded
        If f_Picture.HasFile Then
            Dim myGUID As New System.Guid
            myGUID = Guid.NewGuid()
            newPictureName = myGUID.ToString() + ".jpg"
            f_Picture.SaveAs(Request.MapPath("~/Pictures") & "/" & newPictureName)
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionStringRestaurants").ConnectionString)
        If f_Picture.HasFile Then
            SqlString = "UPDATE Profile SET [FirstName]=@f1,[LastName]=@f2,[Details]=@f3,[Picture]=@f4,[MyFavouriteRestaurant]=@f5,[Hometown]=@f6,[MyFavouriteShop]=@f7,[GlutenfreeSince]=@f8 WHERE [UserName]=@f9"
        Else
            SqlString = "UPDATE Profile SET [FirstName]=@f1,[LastName]=@f2,[Details]=@f3,[MyFavouriteRestaurant]=@f5,[Hometown]=@f6,[MyFavouriteShop]=@f7,[GlutenfreeSince]=@f8 WHERE [UserName]=@f9"
        End If


        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_FirstName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_LastName.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Details.Text)
        If f_Picture.HasFile Then cmd.Parameters.AddWithValue("@f4", "~/Pictures/" & newPictureName)
        cmd.Parameters.AddWithValue("@f5", tb_FavRestaurant.Text)
        cmd.Parameters.AddWithValue("@f6", tb_Hometown.Text)
        cmd.Parameters.AddWithValue("@f7", tb_FavShop.Text)
        cmd.Parameters.AddWithValue("@f8", tb_Glutenfreesince.Text)
        cmd.Parameters.AddWithValue("@f9", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("Profile.aspx?UserName=" & User.Identity.Name)
    End Sub
End Class